package com.eazyshop.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.eazyshop.canal.utils.CanalUtil;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@CanalEventListener
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     *
     * @param eventType 当前操作类型
     * @param rowData 当前操作的数据库的数据
     */
    @ListenPoint(schema = "changgou_business",table = "tb_ad")
    public void addUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){


        System.out.println(eventType.name());
        Map<String, String> beforeMap = CanalUtil.convertToMap(rowData.getBeforeColumnsList());

        System.out.println("修改前的数据:"+beforeMap);
        Map<String, String> afterMap = CanalUtil.convertToMap(rowData.getAfterColumnsList());
        System.out.println("修改后的数据："+afterMap);

        /**
         * 发送消息到rabbitmq中
         */

        rabbitTemplate.convertAndSend("","ad_update",afterMap.get("position"));

    }
}
